package com.jfree.controller;

import com.jfree.entity.general.User;
import com.jfree.feign.ServiceApiFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RequestMapping("/task")
@RestController
public class TaskController {

    @Autowired
    private ServiceApiFeign serviceApiFeign;

    @Value("${server.port}")
    private Integer port;

    @GetMapping("/getUser/{id}")
    public User getUser(@PathVariable("id") Integer id) {
        User user = new User();
        user.setName("task");
        user.setSex(1);
        return serviceApiFeign.user(user);
    }

    @GetMapping("/getException/{id}")
    public String getException(@PathVariable("id") Integer id) {
        return serviceApiFeign.getException(id);
    }

    @GetMapping("/test/{id}")
    public String test(@PathVariable("id") Integer id) {
        User u = new User();
        u.setName("io");
        if (id == 0) {
            return serviceApiFeign.test(0);
        } else if (id == 1) {
            return serviceApiFeign.test1(1);
        } else if (id == 2) {
            return serviceApiFeign.test2(2);
        } else if (id == 3) {
            return serviceApiFeign.test3(3);
        } else if (id == 4) {
            return serviceApiFeign.test4(u).toString();
        } else {
            return serviceApiFeign.test5(u).toString();
        }
    }

    @GetMapping("/zuulErr")
    public String zuulErr() {
        int t = 1 / 0;
        return "ERR";
    }

    @GetMapping("/getPort")
    public Integer getPort() {
        return port;
    }

    @GetMapping("/getSessionId")
    public String getSessionId(HttpServletRequest request) {
        return request.getSession().getId();
    }

}
